#include "rcXMLReader.h"
#include <iostream>

//! Loading the excersices from the XML file
std::set<std::string> rcXMLReader::loadExercises(const char* fileName, bool existExercises)
{
	// Create an empty property tree object
    using boost::property_tree::ptree;
    ptree pt;
	std::set<std::string> entryset;

	try
	{
		// Load the XML file into the property tree. If reading fails
		// (cannot open file, parse error), an exception is thrown.
		read_xml(fileName, pt);

		// Iterate over the config.exercise.lastlayer section and store all found
		// entries in the entries set. The get_child() function
		// returns a reference to the child at the specified path; if
		// there is no such child, it throws. Property tree iterators
		// are models of BidirectionalIterator.
		BOOST_FOREACH(ptree::value_type &v, pt.get_child("config.exercise.lastlayer"))
			entryset.insert(v.second.data());

		existExercises = true;
	}
	catch (...)
	{
		existExercises = false;
	}
	return entryset;
}

// Saves the debug_settings structure to the specified XML file
void rcXMLReader::saveConfiguration(const std::string &fileName, std::set<std::string> configuration)
{
	// Create an empty property tree object
	using boost::property_tree::ptree;
	ptree pt;

	try
	{
		read_xml(fileName, pt);

		// clear any old configuration
		pt.get_child("config.configuration").erase("cube");

		// puts the configuration of the Rubiks Cube into the property tree
		BOOST_FOREACH(const std::string &name, configuration)
			pt.add("config.configuration.cube.entry", name);

		// Write the property tree to the XML file.
		write_xml(fileName, pt);
	}
	catch (...)
	{
	}
}